package simulador;

import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_stat;
import eduni.simjava.Sim_system;

public class Lockers extends Sim_entity {

	private Sim_port in, inDiskControl, out1, out2, out3, out4, out5, out6,
			out7, out8, out9, out10;
	private boolean[] lockers;
	private Sim_stat stat;

	public Lockers(String name) {
		super(name);
		in = new Sim_port("In");
		inDiskControl = new Sim_port("InDiskControl");
		out1 = new Sim_port("Out1");
		out2 = new Sim_port("Out2");
		out3 = new Sim_port("Out3");
		out4 = new Sim_port("Out4");
		out5 = new Sim_port("Out5");
		out6 = new Sim_port("Out6");
		out7 = new Sim_port("Out7");
		out8 = new Sim_port("Out8");
		out9 = new Sim_port("Out9");
		out10 = new Sim_port("Out10");
		lockers = new boolean[] { true, true, true, true, true, true, true,
				true, true, true };
		add_port(in);
		add_port(inDiskControl);
		add_port(out1);
		add_port(out2);
		add_port(out3);
		add_port(out4);
		add_port(out5);
		add_port(out6);
		add_port(out7);
		add_port(out8);
		add_port(out9);
		add_port(out10);
		stat = new Sim_stat();
		stat.add_measure(Sim_stat.ARRIVAL_RATE);
		set_stat(stat);
	}

	public void body() {
		while (Sim_system.running()) {
			Sim_event e = new Sim_event();
			// Get the next event
			sim_get_next(e);
			if (e.get_tag() == -1)
				continue;
			// Get the event's data
			Job job = (Job) e.get_data();
			int entity = e.get_src();
			int cpuID = Sim_system.get_entity_id("CPU");
			int discoID = Sim_system.get_entity_id("Disk");
			Sim_port locker = null;
			if (entity == discoID) {
				switch (job.getRecurso()) {
				case 1:
					lockers[0] = true;
					break;
				case 2:
					lockers[1] = true;
					break;
				case 3:
					lockers[2] = true;
					break;
				case 4:
					lockers[3] = true;
					break;
				case 5:
					lockers[4] = true;
					break;
				case 6:
					lockers[5] = true;
					break;
				case 7:
					lockers[6] = true;
					break;
				case 8:
					lockers[7] = true;
					break;
				case 9:
					lockers[8] = true;
					break;
				case 10:
					lockers[9] = true;
					break;
				default:
					break;
				}
				continue;
			} else if (entity == cpuID) {
				switch (job.getRecurso()) {
				case 1:
					if (lockers[0]) {
						locker = out1;
						lockers[0] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 2:
					if (lockers[1]) {
						locker = out1;
						lockers[1] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 3:
					if (lockers[2]) {
						locker = out1;
						lockers[2] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 4:
					if (lockers[3]) {
						locker = out1;
						lockers[3] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 5:
					if (lockers[4]) {
						locker = out1;
						lockers[4] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 6:
					if (lockers[5]) {
						locker = out1;
						lockers[5] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 7:
					if (lockers[6]) {
						locker = out1;
						lockers[6] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 8:
					if (lockers[7]) {
						locker = out1;
						lockers[7] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 9:
					if (lockers[8]) {
						locker = out1;
						lockers[8] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				case 10:
					if (lockers[9]) {
						locker = out1;
						lockers[9] = false;
					} else {
						sim_putback(e);
						continue;
					}
					break;
				default:
					break;
				}
			} else
				continue;
			// The event has completed service
			sim_completed(e);
			// Even I/O jobs go to disk
			// sim_schedule(out, 0.0, 2, job);
			// sim_schedule(out, 0.0, e.get_tag(), job);
			sim_schedule(locker, 0.0, e.get_tag(), job);
			// Waiting event from Disk
			sim_trace(4, "Job(" + e.get_tag() + ") sent to Disk");
		}
	}

}
